var util = require('../../../../utils/util.js')

var apiHost = wx.getStorageSync("apiHost"); //请求地址
var Appid = wx.getStorageSync('Appid');
var Secret = wx.getStorageSync('Secret');

var AppidFlag = wx.getStorageSync('AppidFlag');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    startDate: util.formatTime2(new Date()),    //开始时间
    endDate: util.formatTime2(new Date()),      //结束时间
    Height: 0,                                  //初始化高度
    startFlag: 0,                               //分页标识 
    pretype : '001',                            //预估累计标识
    proHeight : 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var token = wx.getStorageSync('token');             //用户的token信息

    that.setData({
      token: token
    })
    that.getPreDetail(token,'001',0,10,'','','');
    that.getHeight();
  },

  /**
   * 初始化报表明细
   */
  getPreDetail: function (token, preType, start, length, startTime, endTime, orderNo){
    var that = this;

    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/users/preDetail", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        appId: AppidFlag,
        preType: preType,
        start: start,
        length: length,
        startTime: startTime,
        endTime: endTime,
        orderNo: orderNo
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;

        if (code == "10000") {
          var pro = res.data.data.list;

          var proList = that.separateDecimal(pro);    //分隔小数点
          that.ordersPaging(start,proList);           //分页
  
        } else {
          that.showMsg(msg);
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },


  /**
   * 分隔小数点 （页面显示上用）、切割时间
   */
  separateDecimal: function (proList){
    var that = this;
    for (var i = 0; proList.length > i; i++) {
      var profitAmount = proList[i].profitAmount;
      var orderTime = proList[i].orderTime;
      var proFg = profitAmount.toString().split(".");
      var orderT = orderTime.toString().substr(0,16);

      proList[i].proDq = proFg[0];
      proList[i].proDh = proFg[1];
      proList[i].orderTime = orderT;
    }
    return proList
  },

  /**
   * 下拉
   */
  scrollLower: function () {
    var that = this;
    var start = that.data.startFlag;
    var token = that.data.token;
    var pretype = that.data.pretype;
    
    that.getPreDetail(token, pretype, start, 10, '', '', '')
  },

  /**
   * 商品分页
   */
  ordersPaging: function (start, proData) {
    var that = this;
    if (start > 0) {
      var proList = that.data.proList;
      // 追加数据
      for (var i = 0; i < proData.length; i++) {
        proList.push(proData[i])
      }
      start = parseInt(start) + 1;
    } else {
      var proList = proData;
      start = 1;
    }
    that.setData({
      proList: proList,
      startFlag: start
    })
  },

  /**
   * 报名明细查询
   */
  searchPro : function(){
    var that = this;
    var orderNo = that.data.orderNo;
    var startDate = that.data.startDate;
    var endDate = that.data.endDate;
    var token = that.data.token;
    var pretype = that.data.pretype;

    that.getPreDetail(token, pretype, 0, 10, startDate, endDate, orderNo);
  },

  /**
   * 获取输入框的值
   */
  getInputValue:function(e){
    this.setData({
      orderNo: e.detail.value
    });
  },

  /**
   * 选择器，开始时间
   */
  bindStartDateChange: function (e) {
    this.setData({
      startDate: e.detail.value
    })
  },

  /**
   * 选择器，结束时间
   */
  bindEndDateChange: function (e) {
    this.setData({
      endDate: e.detail.value
    })
  },

  /**
   * 佣金类型切换 tab
   */
  tabPro:function(e){
    var that = this;
    var pretype = e.currentTarget.dataset.pretype;
    var token = that.data.token;
    that.setData({
      pretype: pretype
    })
    that.getPreDetail(token, pretype, 0, 10, '', '', '')
  },

  /**
   * 购物车单项高度
   */
  getHeight: function () {
    var that = this;
    var query = wx.createSelectorQuery();

    var reportDetailTopConHeight = "";

    query.select('#reportDetailTopCon').boundingClientRect()
    query.exec(function (res) {
      reportDetailTopConHeight = res[0].height;
      wx.getSystemInfo({
        success: function (res) {
          that.setData({
            proHeight: res.windowHeight - reportDetailTopConHeight
          });
        }
      });
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 提示信息
   */
  showMsg: function (msg) {
    wx.showToast({
      title: msg,
      icon: 'none',
      duration: 2000,
      mask: true
    });
  },
})